﻿using System;

namespace HYCodeBase.Math
{
	public static class MathUtility
	{
		public static double GetPrecision(int nDegree)
		{
			return 1.0 / System.Math.Pow(10.0, (double)nDegree);
		}

		public static bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		public static bool LessThan(double val1, double val2, double eps)
		{
			return val1 - val2 < -eps;
		}

		public static bool GreaterThan(double val1, double val2)
		{
			return val1 - val2 > 1E-09;
		}

		public static bool GreaterThan(double val1, double val2, double eps)
		{
			return val1 - val2 > eps;
		}

		public static bool IsEqual(double val1, double val2)
		{
			return !MathUtility.LessThan(val1, val2) && !MathUtility.LessThan(val2, val1);
		}

		public static bool IsEqual(double val1, double val2, double eps)
		{
			return !MathUtility.LessThan(val1, val2, eps) && !MathUtility.LessThan(val2, val1, eps);
		}

		public static bool Lessthan_Or_Equal(double val1, double val2)
		{
			return !MathUtility.GreaterThan(val1, val2);
		}

		public static bool Lessthan_Or_Equal(double val1, double val2, double eps)
		{
			return !MathUtility.GreaterThan(val1, val2, eps);
		}

		public static bool Greaterthan_Or_Equal(double val1, double val2)
		{
			return !MathUtility.LessThan(val1, val2);
		}

		public static bool Greaterthan_Or_Equal(double val1, double val2, double eps)
		{
			return !MathUtility.LessThan(val1, val2, eps);
		}

		public static double formatAngle(double angle)
		{
			int num = (int)(angle / System.Math.PI*2);
			double num2 = angle - (double)num * System.Math.PI*2;
			if (num2 < 0.0)
			{
				num2 += System.Math.PI*2;
			}
			return num2;
		}

		public static double BetweenTheAngles(double dSAngle, double dEAngle, bool range2PI)
		{
			dSAngle = MathUtility.formatAngle(dSAngle);
			dEAngle = MathUtility.formatAngle(dEAngle);
			if (System.Math.Abs(dEAngle - dSAngle) < 1E-05)
			{
				return 0.0;
			}
			if (range2PI && dEAngle < dSAngle)
			{
				dEAngle += System.Math.PI*2;
			}
			return dEAngle - dSAngle;
		}

		public static bool IsEqualAngles(double angle1, double angle2)
		{
			return MathUtility.IsEqual(angle1, angle2, 0.0001) || MathUtility.IsEqual(System.Math.Abs(angle1 - angle2), System.Math.PI*2, 0.0001);
		}

		public static double AoArea(int count, double[] X, double[] Y)
		{
			if (count < 3)
			{
				return 0.0;
			}
			double num = Y[0] * (X[count - 1] - X[1]);
			for (int i = 1; i < count; i++)
			{
				num += Y[i] * (X[i - 1] - X[(i + 1) % count]);
			}
			return num / 2.0;
		}

		public static double TriangleArea(double edgeALength, double edgeBLength, double edgeCLength)
		{
			double num = (edgeALength + edgeBLength + edgeCLength) / 2.0;
			return System.Math.Sqrt(num * (num - edgeALength) * (num - edgeBLength) * (num - edgeCLength));
		}

		public static bool IsOdd(int nSource)
		{
			return nSource / 2 * 2 != nSource;
		}

		public const double _epsDouble = 1E-09;

		public const double _epsAngle = 0.0001;
	}
}
